#! /usr/bin/env python
#coding=utf-8

import time

from google.appengine.api import urlfetch
from google.appengine.ext import db

#from Log import Log

class apiConfig(db.Model):
	"""
    定义一个数据模型类
    TODO 如果数据是空的时候，需要处理方法。
    """
	appkey = db.StringProperty()
	secret = db.StringProperty()
	url = db.StringProperty()
	v = db.StringProperty()
	created = db.DateTimeProperty(auto_now_add=True)

class GAEHttp(object):
	"获取http响应"
	def get(self, url):
		result = urlfetch.fetch(url)
#		reference:http://code.google.com/intl/zh-CN/appengine/docs/python/urlfetch/responseobjects.html
		rsp = HttpRsp(result.status_code, result.headers, result.content)
		return rsp


class HttpRsp(object):
	"包含默认为空值的类"
	def __init__(self, statusCode=None, headers=None, content=None):
		self.statusCode = statusCode
		self.content = content
		self.headers = headers

class HttpClient(object):
	"更新的http response"
    def get(self,url):
		Log.debug("req url:" + url)
        rsp = HttpRsp()
        result = urlfetch.fetch(url)
        if result.status_code == 200:
            rsp.content = result.content
		Log.debug("http rsq\nstatusCode:" + str(rsp.statusCode) + "\ncontent:" + str(rsp.content))
        return rsp


class taobaoApi(object):
	ISOTIMEFORMAT= '%Y-%m-%d %X'

    def __init__(self, apiConfig):
		pass

    def __now__(self):
    	stime = None
    	if(time.timezone == 0):
    		gmtimefix = 28800
    		stime = time.gmtime(gmtimefix + time.time())
    	else:
    		stime = time.localtime()
    	strtime = time.strftime( TbApi.ISOTIMEFORMAT, stime)
    	return strtime
